package com.yami.shop.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.model.Msg;
import com.yami.shop.bean.model.MsgSub;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.MsgSubService;
import com.yami.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * (MsgSub)表控制层
 *
 * @author rows
 * @since 2023-04-11 09:46:57
 */
@RestController
@RequestMapping("/p/msgSub")
@RequiredArgsConstructor
public class MsgSubController {
    /**
     * 服务对象
     */
    private final MsgSubService msgSubService;
    private final UserService userService;

    @GetMapping("/list")
    @Operation(summary = "获取留言列表" , description = "获取留言列表")
    public ServerResponseEntity list(Page<MsgSub> page,MsgSub msgSub) {
        return ServerResponseEntity.success(msgSubService.selectPage(page,msgSub));
    }

    /**
     * 新增数据
     *
     * @param msgSub 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增", description = "新增")
    @PostMapping("add")
    public ServerResponseEntity add(@Valid @RequestBody MsgSub msgSub) {
        msgSub.setUserId(SecurityUtils.getUser().getUserId());
        msgSub.setUserType(0);
        User user=userService.getUserByUserId(msgSub.getUserId());
        msgSub.setUserName(user.getNickName());
        return ServerResponseEntity.success(this.msgSubService.save(msgSub));
    }

}